package com.j180.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet(value = "/cookie")
public class CookieServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("CookieServlet====>>>>");
        //创建一个cookie (键值对)
        //cookie 只能存放字符串
        //cookie的默认有效期为 会话结束时（....）
        Cookie  cookie = new Cookie("user","j180");
         //设置cookie的有效期（以秒为单位）
        cookie.setMaxAge(5*60);
        Cookie  cookieTime = new Cookie("date", LocalDateTime.now().toString());
        //把cookie添加进响应头
        resp.addCookie(cookie);
        resp.addCookie(cookieTime);

        resp.getWriter().print("ok");


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req,resp);
    }
}
